<?php
class Controller_Admin_Merchant extends Controller_Template
{

	private $_limit;
	public function __construct($request, $response)
	{
		parent::__construct($request, $response);
	
		$this->_limit	= Kohana::config('merchant.page_size');
		$this->template = 'smarty:admin/merchant/index';
		$this->_sphinx = new Model_Sphinx();
	}

	public function action_index()
	{
		$this->action_search();	
	}

	/**
	 * 所有商铺当前在线统计
	 */
	public function action_current_online ()
	{
	}

	/**
	 * 商铺IM数据入口
	 */
	public function action_im ($target_type = NULL, $target_id = NULL)
	{
		// im 当日统计数据总览
		if ($target_type == 'today' || empty($target_type))
		{
			$this->template->set_filename('smarty:admin/statis/im/today');
			$this->template->set('page_title', '今日商铺在线汇总');
		}
		else if ($target_type == 'online')
		{
			$day = $target_id;
			if (NULL !== $day)
				$day = date('Y-m-d', strtotime($day));
			else
				$day = date('Y-m-d');

			$this->template->set_filename('smarty:admin/statis/im/online');
			$this->template->set('page_title', '所有商铺在线汇总');
			$this->template->set('day', $day);
		}
		// im 商铺日详情
		else if (is_numeric($target_type))
		{
			$shop_id = (int) $target_type;
			$day     = empty($target_id) ? date("Ymd") : $target_id;
			$daystr  = date("Y-m-d", strtotime($day));

			$im      = new IM_Oversee;
			$detail  = $im->detail ($shop_id, $day);

			$this->template->set_filename('smarty:admin/statis/im/detail');
			$this->template->set('day',     $daystr);
			$this->template->set('shop_id', $shop_id);
			$this->template->set('detail',  $detail);
		}
		else
		// 所有商铺IM在线情况
		{
			$this->current_all_im ();
		}
	}

	/**
	 * 重做
	 */
	private function current_all_im ()
	{
		$shop_arr = $this->_sphinx->get_shop_list($query,0,100); 
		if(!empty($shop_arr))
		{
			$total_count = intval($shop_arr['count']);
			$shop_ids    = $shop_arr['ids'];
		}
		$contacts    = Model_ShopInfo::get_shopkeeper($shop_arr['ids'],1);
		foreach ($shop_ids as $shop_id)
		{
			$shop_info     = shop::shop_info($shop_id);
			$shop_info['level_show' ]   = $shop_comments_score['level_html']['html'];
			$shop_info['contact']      = $contacts[$shop_id];
			$shops[] = $shop_info;
		}

		$this->template->set_filename('smarty:admin/statis/im/all_im');
		$this->template->set('shops', $shops);
	}

	
	public function action_search()
	{
		if(strval($this->request->query('login_name')))
			$passport	= strval($this->request->query('login_name'));

		if($passport == '填写商家姓名或者账号')
			$passport	= '';
		
		$condition['passport']	= $passport;

		$condition['order_by']	= array('id' => 'desc');	
		
		$condition['open_status'] = intval($this->request->query("shop_status"));

		$page			= isset($_GET['page']) ? intval($_GET['page']) : 1;

		$offset			= $this->_limit * ($page - 1);
	
		$data			= Model_Shopinfo::shop_lists($condition, $offset, $this->_limit);
	
		$count			= Model_Shopinfo::count_shop_lists($condition);
		
		$page_config	= array(
							'total_items'		=> $count,
							'items_per_page'	=> $this->_limit,
				);
		for($i=0 ; $i < count($data); $i++)
		{
			$data[$i]['create_time']	= date('Y-m-d H:i:s' , $data[$i]['apply_time']);

			$data[$i]['status_name']	= (intval($data[$i]['status']) & 1) > 0 ? '已激活' : '未激活';
		
			$data[$i]['shop_id']		= $data[$i]['id'];

			$open						= $data[$i]['open_status'];

			$shop_status				= Model_Shopinfo::has_verify_merchant($data[$i]['id']);
			
			$data[$i]['open_status']	= $open ? '已开通' : '未开通';

			$data[$i]['shop_status']	= $shop_status;

		}
		$pagination		= Pagination::factory($page_config);
		
		$this->template->set_filename('smarty:admin/merchant/list');
		$this->template->set('total', $count);
		$this->template->set('data', $data);
		$this->template->set('login_name', $condition['passport']);
		$this->template->set('status', $condition['open_status']);
		$this->template->set('page_title', '商家管理列表');
		$this->template->set('pagination', $pagination->render());
		$function_name	= Request::$current->controller();
		$this->template->set('function_name', $function_name);
		$this->template->set('sub_domain', Site::domain('manager'));
		$this->response->body($this->template->render());
	}

	public function action_del_merchant()
	{
		if(isset($_POST['action']) && $_POST['action'] == 'del')
		{
			$page = 1;
			$arr_del = array();
			foreach($_POST as $key => $value)
			{
				if(strpos($key,'u_') === 0)
				{
					$key = explode('_',$key);
					$arr_del[] = $key[1];
				}
			}
			//print_r($arr_del);
			//删除会员
		}
		//成功提示信息
		$url = '/merchant/';
		$this->request->redirect($url, $code = 302);
	}

	public function action_close()
	{
		if($this->request->is_ajax())
		{
			$shopid	= $this->request->post('shop_id');

			$shop_status	= $this->request->post('shop_status');

			$status	= Kohana::config('shopinfo.open_status');

			$rs		= Model_Shopinfo::set_open($shopid, $status[$shop_status]);

			echo empty($rs) ? '操作成功' : '操作失败';

			exit;
		}
	}
}

?>
